home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6229 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  67 lines

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Functions as parameters
  5. Date: Fri, 23 Feb 1996 07:09:26 GMT
  6. Organization: Netcom
  7. Message-ID: <312d6726.112558110@nntp.ix.netcom.com>
  8. References: <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
  9. NNTP-Posting-Host: ix-dc6-15.ix.netcom.com
  10. X-NETCOM-Date: Thu Feb 22 11:09:21 PM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. clgonsal@undergrad.math.uwaterloo.ca (Carl Laurence Gonsalves) wrote:
  14.  
  15. > I've often seen and even written code that uses function pointers like
  16. > this:
  17. > /* cut here */
  18. > #include <stdio.h>
  19. > void foo( int (*func)( int ) ){
  20. >   printf("%d\n", func( 5 ) );
  21. > }
  22. > int bar( int x ){
  23. >   return x*2;
  24. > }
  25. > int
  26. > main(){
  27. >   foo( bar );
  28. >   return 0;
  29. > }
  30. > /* cut here */
  31. > Recently though, I saw a similar piece of code that did something like
  32. > this:
  33. > /* cut here */
  34. > void foo( int func( int ) ){
  35. >   printf("%d\n", func( 5 ) );
  36. > }
  37. > /* cut here */
  38. > I tried compiling this with both Watcom C 10.0 and GNU C 2.6.3, and it
  39. > worked. So my question is: is this standard, or is this some weird compiler
  40. > extension. I've never seen this syntax before. Does it mean the same thing
  41. > as the first piece of code? The body of the two functions is identical, and
  42. > they're called in the same way.
  43. > If they are identical, why is the (*func)(int) syntax so much more common?
  44. > The func(int) syntax does seem easier to type as well as read, but I've 
  45. > never seen it before.
  46.  
  47. Either is legal.  I'm not sure why the pointer notation is more
  48. common; probably mainly habit.
  49.  
  50. Note that the type of the parameter is pointer to function regardless
  51. of how you write it.  The compiler silently treates a function
  52. declaration in a parameter list to a pointer to function declaraton
  53. similar to the way it converts an array declaration to a pointer
  54. declaration.
  55.  
  56. Michael M Rubenstein
  57.